<?php
require('config.php');
$link = mysql_connect($config['mysql_host'], $config['mysql_username'], $config['mysql_password']);
mysql_select_db($config['mysql_database']);
$action = $_POST['action'];
$computer_id = $_POST['computer_id'];
if(!ereg('^[A-Za-z0-9]*$', $computer_id) || $computer_id == '')
{
	die('Error');
}
$result = mysql_query('select * from computers where computer_id="'.$computer_id.'"');
if(mysql_num_rows($result) == 0)
{
	die('Error');
}
$computer = mysql_fetch_assoc($result);
switch($action)
{
	case 'ping':
		if($computer['active'] == true)
		{
			mysql_query('insert into logs values(0, "'.$computer_id.'", '.time().', "'.$_SERVER['REMOTE_ADDR'].'")');
			echo '|active|';
		}
		else
		{
			echo '|false|';
		}
		break;
	case 'upload':
		if(!$_POST['file_name'] || !$_POST['file_contents'])
		{
			die('Error');
		}
		if(substr($_POST['file_name'], 0, 7) == 'screen-')
		{
			$type = 0;
		}
		else
		{
			$type = 1;
		}
		$name = $computer_id.'-'.basename($_POST['file_name'], '.png').'.png';
		mysql_query('insert into images values(0, "'.$computer_id.'", "'.$name.'", '.$type.', '.time().')');
		file_put_contents('images/'.$name, base64_decode($_POST['file_contents']));
		break;
	default:
		die('Error');
}
?>